#include "Sprite.h"


CSprite::CSprite(const char * fileName)
{
	GLfloat quadCoords[] = { -.5, -.5, 0, -.5, .5, 0, .5, -.5, 0, .5, .5, 0};
	GLfloat texCoords[] =  { 0, 0 , 1, 0, 0, 1, 1, 1}; 
	textureQuad.Begin(GL_TRIANGLE_STRIP, 4, 4);
	textureQuad.CopyVertexData3f(quadCoords);
	textureQuad.CopyTexCoordData2f(texCoords, 0);
	textureQuad.End();

	glGenTextures(1, &textureInt);
	glBindTexture(GL_TEXTURE_2D, textureInt);
	LoadBMPTexture(fileName,    GL_LINEAR_MIPMAP_LINEAR, GL_REPEAT);
}


CSprite::~CSprite(void)
{
}

void CSprite::LoadBMPTexture(const char * Filename, GLenum filter, GLenum wrap)
{
	GLbyte * pBits;
	int width, height;
	pBits = gltReadBMPBits(Filename, &width, &height);

	if(pBits == nullptr)
	{
		MessageBox(NULL, "Cannot load texture", Filename, MB_OK);
		return;
	}

	glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, pBits);
	free(pBits);
	
	if(filter != GL_LINEAR && filter != GL_NEAREST)
		glGenerateMipmap(GL_TEXTURE_2D);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap);
}
